package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.user.modules.models.constant.UserConstant;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import redis.clients.jedis.ScanParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解除用户限制
 * @author .
 */
@IocBean
@At("/platform/user/manager/resetLimit")
public class UserResetLimitController {
    private static final Log log = Logs.get();

    @Inject
    private RedisService redisService;

    @At("/")
    @Ok("beetl:/platform/user/manager/resetLimit/index.html")
    @RequiresPermissions("user.manager.resetLimit.list")
    public void index() {

    }


    /**
     * 查询被限制登录的用户
     *
     * @return
     */
    @At()
    @Ok("json")
    @RequiresPermissions("user.manager.resetLimit.list")
    public Object loginLimitUsers(@Param("userName") String userName,
                                  @Param("pageNumber") int pageNumber,
                                  @Param("pageSize") int pageSize) {
        ScanParams scanParams = new ScanParams();
        scanParams.match(UserConstant.REDIS_KEY_LOGIN_ERR + "*");
        scanParams.count(Integer.MAX_VALUE);
        List<String> keys = redisService.scan("0", scanParams).getResult();
        List<Map<String, String>> list = new ArrayList<>();
        for (String key : keys) {
            String value = redisService.get(key);
            String name = key.replace(UserConstant.REDIS_KEY_LOGIN_ERR, "");
            // 筛选条件：次数为0的用户
            boolean cnd = "0".equals(value);
            // 按用户名搜索
            if (Strings.isNotBlank(userName)) {
                cnd = cnd && name.equals(userName);
            }
            if (cnd) {
                HashMap<String, String> map = new HashMap<>();
                map.put("userName", name);
                map.put("canLoginNum", value);
                list.add(map);
            }
        }

        int endIndex = pageNumber == 0 ? pageSize : pageNumber * pageSize;
        int count = list.size();
        int startIndex = endIndex - pageSize;
        list = list.subList(pageNumber == 0 ? 0 : startIndex, endIndex == 0 ? count : Math.min(endIndex, count));
        // 设置分页属性
        Pagination pagination = new Pagination();
        pagination.setTotalCount(count);
        pagination.setPageNo(pageNumber);
        pagination.setPageSize(pageSize);
        pagination.setList(list);
        return Result.success().addData(pagination);
    }


    /**
     * 重置用户输错密码限制登录次数(登录功能用户输错5次限制明日才能登录)
     *
     * @return
     */
    @At()
    @Ok("json")
    @RequiresPermissions("user.manager.resetLimit.edit")
    public Object resetLoginLimit(@Param("userName") String userName) {
        // 重置用户登录限制次数
        redisService.del(UserConstant.REDIS_KEY_LOGIN_ERR + userName);
        log.info("重置用户登录次数限制：" + userName);
        return Result.success();
    }

}
